﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Meqs.Entity;
using Meqs.Entity.Views;
using Meqs.Logic.Interface;
using Meqs.Utilities;

public partial class Credit_Teacher_Reprots_TestScoreState : ContainerBasePage
{

    Dictionary<int, List<int>> dicMpc = new Dictionary<int, List<int>>();//保存了班级和科目的id

    protected void Page_Load(object sender, EventArgs e)
    {
        InitDisplay();

        btnQuery_Click(btnQuery as object, new EventArgs());
    }

    /// <summary>
    /// 初始化界面标签元素的显示行为
    /// </summary>
    protected void InitDisplay()
    {
        // 日期选择下拉框行为
        BindSchoolYear();

        // 年级选择下拉框行为
        BindGrade();
    }

    /// <summary>
    /// 查询按钮事件
    /// </summary>
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        int nYear = 0;
        int nTerm = 0;
        int nGrade = 0;
        //String date;

        string[] outString = selYear.SelectedValue.Split(new char[] { ',' });
        if (outString.Length != 2)
            return;

        if (!Int32.TryParse(outString[0], out nYear))
            return;

        if (!Int32.TryParse(outString[1], out nTerm))
            return;

        if (!Int32.TryParse(selGrade.SelectedValue, out nGrade))
            return;

        // 查找年级内班级
        Hashtable ht = new Hashtable();
        IList<VClsInfo> clsList = null;
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("Grade", nGrade.ToString());
        ht.Add("SchoolYear", nYear);
        clsList = Container<IClsInfoLogic>.Instance.FindClassInfoByCauseID(ht);

        if (clsList == null || clsList.Count < 1)
            return;
 
        IList<int> idList = new List<int>();
        foreach (VClsInfo info in clsList)
        {
            idList.Add(info.RID);
        }

        IList<GlobalCauseInfo> subjectList = Container<IGlobalCauseInfoLogic>.Instance.SelectSchoolCause(SchoolMark, 1);


        ht = new Hashtable();
        ht.Add("Grade", nGrade);
        ht.Add("SchoolMark", SchoolMark);
        IList<CouseGradeInfo> cgList = Container<ICouseGradeInfoLogic>.Instance.FindAll(ht);

        List<GlobalCauseInfo> sl = new List<GlobalCauseInfo>();
        foreach (GlobalCauseInfo info in subjectList)
        {
            foreach (CouseGradeInfo gradeInfo in cgList)
            {
                if (gradeInfo.CourseID == info.RID)
                {
                    sl.Add(info);
                    break;
                }
            }
        }
        subjectList = sl;


        //#region 过滤科目权限
        
        //if (!SchoolCommon.CheckRole((int)RoleKeyEnum.School_Header, nYear, nTerm))
        //{

        //    bool canUseManage = SchoolCommon.CheckRole((int)RoleKeyEnum.Class_Header, nYear, nTerm);
        //    if (canUseManage) //若是班主任
        //    {
        //        List<int> allCauseID = new List<int>();
        //        foreach (GlobalCauseInfo g in subjectList)
        //        {
        //            allCauseID.Add(g.RID);
        //        }

        //        IList<ClsInfo> listClsTemp = Container<IClsInfoLogic>.Instance.SelectHisClsInfoByHeader(nYear,
        //                                                                                                Common.
        //                                                                                                    GetUserInfo()
        //                                                                                                    .UID);
        //        foreach (ClsInfo item in listClsTemp)
        //        {
        //            //if(!canViewCls.Contains(item.RID))
        //            //    canViewCls.Add(item.RID);

        //            if(!dicMpc.ContainsKey(item.RID))
        //                dicMpc.Add(item.RID, allCauseID);
        //        }

        //        //foreach (GlobalCauseInfo g in listCause)
        //        //{
        //        //    if(!canViewSubject.Contains(g.RID))
        //        //        canViewSubject.Add(g.RID);
        //        //}
        //    }

        //    canUseManage = SchoolCommon.CheckRole((int)RoleKeyEnum.Class_Teacher, nYear, nTerm);
        //    if (canUseManage)
        //    {
        //        //若是任课老师
        //        IList<TeachClsCause> listTcc =
        //            Container<ITeachClsCauseLogic>.Instance.SelectHisTech(Common.GetUserInfo().UID, nYear);
        //        foreach (TeachClsCause item2 in listTcc)
        //        {
        //            //if (!canViewCls.Contains(item2.ClsID))
        //            //    canViewCls.Add(item2.ClsID);

        //            //if (!canViewSubject.Contains(item2.CauseID))
        //            //    canViewSubject.Add(item2.CauseID);

        //            List<int> tempG = new List<int>();

        //            tempG.Add(item2.CauseID);
        //            if (!dicMpc.ContainsKey(item2.ClsID))
        //                dicMpc.Add(item2.ClsID, tempG);
        //        }
        //    }
 
        //}

        //#endregion

        IList<ClassResultExamList> examList = null;
        ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        ht.Add("SchoolYear", nYear);
        ht.Add("Term", nTerm);
        //ht.Add("CauseID", nSubject);
        examList = Container<IClassResultExamListLogic>.Instance.FindAll(ht);

        IList<TestScoreInTermView> moralList = Container<IClassResultExamListLogic>.Instance.FindMoralInTerm(SchoolMark, nYear, nTerm, nGrade);
        //IList<StuCauseEvalState> stateList = null;

        //    stateList =
        //        Container<IStuCauseEvalHistoryLogic>.Instance.GetStateWithDate(nYear, nTerm, SchoolMark, idList, date);
        //    ht = new Hashtable();
        //    ht.Add("SchoolMark", SchoolMark);
        //    ht.Add("SchoolYear", nYear);
        //    ht.Add("Term", nTerm);



        CreateTable(clsList, examList, subjectList,moralList, nYear, nTerm);


    }

    /// <summary>
    /// 构造结果表格(历史)
    /// </summary>
    private void CreateTable(IList<VClsInfo> classInfo, IList<ClassResultExamList> scoreList, IList<GlobalCauseInfo> subjectList, IList<TestScoreInTermView> moralList,
                            int year, int term)
    {
        List<int> gcList = SchoolCommon.CanViewSubjects((int)SecurityTable.Security.Credit_CanView, year, term);
        if (gcList != null)
        {
            IList<GlobalCauseInfo> gcList1 = Container<ITeachClsCauseLogic>.Instance.GetTeacherCourse(Common.GetUserInfo().UID, SchoolMark);
            subjectList.Clear();
            foreach (GlobalCauseInfo h in gcList1)
            {

                subjectList.Add(h);
            }
        }

        List<int> enableSubjectList = SchoolCommon.CanViewClasses((int)SecurityTable.Security.Credit_CanView, year, term);
        if (enableSubjectList != null)
        {
            IList<VClsInfo> idList = new List<VClsInfo>();
            foreach (VClsInfo info in classInfo)
            {
                if (enableSubjectList.Contains(info.RID))
                    idList.Add(info);
            }
            classInfo = idList;
        }





        tbResult.Rows.Clear();
        int width = 90;
        TableHeaderRow hrow = new TableHeaderRow();
        TableHeaderCell hcell = new TableHeaderCell();
        hcell.Text = "班级";
        hcell.Style[HtmlTextWriterStyle.Width] = "90px";
        hcell.CssClass = "FixedRowCol";
        hrow.Cells.Add(hcell);

        // 构造表头
        foreach (GlobalCauseInfo info in subjectList)
        {
            hcell = new TableHeaderCell();
            hcell.Text = info.CauseName;
            hcell.Style[HtmlTextWriterStyle.Width] = "75px";
            width += 75;
            hcell.CssClass = "FixedRow";
            hrow.Cells.Add(hcell);
        }

        //hcell = new TableHeaderCell();
        //hcell.Text = "德育表现";
        //hcell.Style[HtmlTextWriterStyle.Width] = "75px";
        //width += 75;
        //hcell.CssClass = "FixedRow";
        //hrow.Cells.Add(hcell);

        tbResult.Rows.Add(hrow);
        tbResult.Style[HtmlTextWriterStyle.Width] = String.Format("{0}px", width);
        tbResult.CssClass = "useList1";

        // 填充表内容
        TableRow row = null;
        TableCell cell = null;
        foreach (VClsInfo info in classInfo)
        {
            row = new TableRow();
            cell = new TableCell();
            cell.Text = info.ClsGradeName;
            cell.CssClass = "FixedCol";
            row.Cells.Add(cell);
            //cell = new TableCell();
            //cell.Text = info.Header;
            //row.Cells.Add(cell);

            ////过滤科目
            //List<int> causeIds = new List<int>();
            //foreach (KeyValuePair<int, List<int>> item3 in dicMpc)
            //{
            //    if (item3.Key == info.RID)
            //    {
            //        causeIds.AddRange(item3.Value);
            //    }
            //} 
        

            foreach (GlobalCauseInfo cause in subjectList)
            {
                cell = new TableCell();
                bool sign = false;

                //if (causeIds.Contains(cause.RID))
                //{


                    foreach (ClassResultExamList state in scoreList)
                    {

                        if (state.ClassID == info.RID && state.CauseID == cause.RID)
                        {
                            // 构造超链html标签
                            //HyperLink control = new HyperLink();
                            //control.Text = "已录入";
                            //cell.Controls.Add(control);
                            cell.Text = "已录入";
                            sign = true;
                            break;
                        }
                    }
                    if (!sign)
                    {
                        IList<ClassTercher> classteachers = Container<ITeachClsCauseLogic>.Instance.FindClassTercherEx(info.RID, cause.RID);
                        if (classteachers.Count > 0)
                        {
                            cell.Text = classteachers[0].TeachName;
                        }
                        else
                        {
                            cell.Text = "未录入";
                        }
                        cell.Style[HtmlTextWriterStyle.Color] = "Red";
                    }
                //}
                //else
                //{
                //    cell.Text = "";
                   
              
                //}

                row.Cells.Add(cell);
            }
            //cell = new TableCell();
            //bool s = false;
            //foreach (TestScoreInTermView moral in moralList)
            //{
            //    if (info.RID == moral.ClsID) 
            //    {
            //        cell.Text = "已录入";
            //        s = true;
            //        break;
            //    }
            //}
            //if (!s)
            //{
            //    cell.Text = "未录入";
            //    cell.Style[HtmlTextWriterStyle.Color] = "Red";
            //}
            //row.Cells.Add(cell);
            tbResult.Rows.Add(row);
        }
    }

    /// <summary>
    /// 绑定学期下拉框
    /// </summary>
    protected void BindSchoolYear()
    {
        //Hashtable ht = new Hashtable();
        //ht.Add("SchoolMark", SchoolMark);
        //IList<StuCauseEvalHistory> historyList = Container<IStuCauseEvalHistoryLogic>.Instance.FindAll(ht);
        //if (historyList == null || historyList.Count < 1)
        //{
        //    selYear.Items.Add(new ListItem("没有历史数据", "-1"));
        //}
        //IList<String> strList = new List<String>();
        //foreach (StuCauseEvalHistory history in historyList)
        //{
        //    String value = String.Format("{0},{1}", history.SchoolYear, history.Term);
        //    if (!strList.Contains(value))
        //    {
        //        int year = 0;
        //        int term = 0;
        //        if (!Int32.TryParse(history.SchoolYear.ToString(), out year))
        //            return;
        //        if (!Int32.TryParse(history.Term.ToString(), out term))
        //            return;
        //        String str = Common.ConvertYearAndTerm(year, term);
        //        selYear.Items.Add(new ListItem(str, value));
        //        strList.Add(value);
        //    }
        //}
        SchoolInfo si = SchoolCommon.Sch_SchoolInfo;
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", si.SchoolMark);
        IList<TermInfo> terms = Container<ITermInfoLogic>.Instance.FindAll(ht);
        foreach (TermInfo term in terms)
        {
            String str = Common.ConvertYearAndTerm(term.SchoolYear, term.Term);
            String value = String.Format("{0},{1}", term.SchoolYear, term.Term);
            selYear.Items.Add(new ListItem(str, value));
        }
    }

    /// <summary>
    /// 绑定年级下拉框
    /// </summary>
    protected void BindGrade()
    {
        Hashtable ht = new Hashtable();
        ht.Add("SchoolMark", SchoolMark);
        IList<SchoolInfo> schoolList = Container<ISchoolInfoLogic>.Instance.FindAll(ht);
        switch (schoolList[0].SchoolOpt)
        {
            case 1:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                break;
            case 2:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 3:
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
            case 4:
                selGrade.Items.Add(new ListItem("一年级", "1"));
                selGrade.Items.Add(new ListItem("二年级", "2"));
                selGrade.Items.Add(new ListItem("三年级", "3"));
                selGrade.Items.Add(new ListItem("四年级", "4"));
                selGrade.Items.Add(new ListItem("五年级", "5"));
                selGrade.Items.Add(new ListItem("六年级", "6"));
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                break;
            case 5:
                selGrade.Items.Add(new ListItem("初一", "7"));
                selGrade.Items.Add(new ListItem("初二", "8"));
                selGrade.Items.Add(new ListItem("初三", "9"));
                selGrade.Items.Add(new ListItem("高一", "10"));
                selGrade.Items.Add(new ListItem("高二", "11"));
                selGrade.Items.Add(new ListItem("高三", "12"));
                break;
        }
    }

    [WebMethod]
    public static string GetExcel(ExcelTableCell[][] cells)
    {
        string temp_path = HttpContext.Current.Server.MapPath("../../../");
        string save_path = temp_path + "\\download\\" + String.Format("{0}_{1}_down.xls", Common.GetUserInfo().SchoolName, Common.GetUserInfo().UName);
        HttpContext.Current.Session["filepath"] = save_path;
        Meqs.Utilities.ExcelUtil.ExcelExport(save_path, "表格", 1, 0, cells);
        return "";
    }
}
